package cn.qayy.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
import cn.qayy.utils.ServletContextUtils;
import cn.qayy.utils.StringUtil;

/***
 * @author wy 
 * 基础库
 * 备注：比如：如果操作5条，成功3条，说明还有2条是在当前情况下，存在重复项
 */

@SuppressWarnings("all")
public class BasisDTO implements Serializable,Uploadable{
	/*** 经理 **/
	private String mgr = "";
	/*** 大区 **/ 
	private String daqu = "";
	/** 片区 ****/
	private String space = "";
	/***
	 * 省份
	 */
	private String province = "";
	/**
	 * 地市
	 */
	private String city = "";
	/**
	 * 区县
	 */
	private String country = "";
	/**
	 * 分组标识
	 */
	private String product = "";
	/**
	 * 修订标识
	 */
	private String isUpdate = "E";
	/**
	 * id
	 */
	private String id = "";
	
	/****
	 * 重写hashCode/equals province+city+country+product 构成唯一的对象
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((city == null) ? 0 : city.hashCode());
		result = prime * result + ((country == null) ? 0 : country.hashCode());
		result = prime * result + ((product == null) ? 0 : product.hashCode());
		result = prime * result + ((province == null) ? 0 : province.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BasisDTO other = (BasisDTO) obj;
		if (city == null) {
			if (other.city != null)
				return false;
		} else if (!city.equals(other.city))
			return false;
		if (country == null) {
			if (other.country != null)
				return false;
		} else if (!country.equals(other.country))
			return false;
		if (product == null) {
			if (other.product != null)
				return false;
		} else if (!product.equals(other.product))
			return false;
		if (province == null) {
			if (other.province != null)
				return false;
		} else if (!province.equals(other.province))
			return false;
		return true;
	}
	public BasisDTO() {
		super();
	}
	public BasisDTO(String mgr, String daqu, String space, String province, String city, String country, String product,
			String id) {
		super();
		this.mgr = mgr;
		this.daqu = daqu;
		this.space = space;
		this.province = province;
		this.city = city;
		this.country = country;
		this.product = product;
		this.id = id;
	}
	
	public BasisDTO(String mgr, String daqu, String space, String province, String city, String country, String product,
			String isUpdate, String id) {
		super();
		this.mgr = mgr;
		this.daqu = daqu;
		this.space = space;
		this.province = province;
		this.city = city;
		this.country = country;
		this.product = product;
		this.isUpdate = isUpdate;
		this.id = id;
	}
	public String getMgr() {
		return mgr;
	}
	public void setMgr(String mgr) {
		this.mgr = mgr;
	}
	public String getDaqu() {
		return daqu;
	}
	public void setDaqu(String daqu) {
		this.daqu = daqu;
	}
	public String getSpace() {
		return space;
	}
	public void setSpace(String space) {
		this.space = space;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getProduct() {
		return product;
	}
	public void setProduct(String product) {
		this.product = product;
	}
	public String getIsUpdate() {
		return isUpdate;
	}
	public void setIsUpdate(String isUpdate) {
		this.isUpdate = isUpdate;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	@Override
	public String toString() {
		return "BasisDTO [mgr=" + mgr + ", daqu=" + daqu + ", space=" + space + ", province=" + province + ", city="
				+ city + ", country=" + country + ", product=" + product + ", isUpdate=" + isUpdate + ", id=" + id
				+ "]";
	}
	@Override
	public Uploadable filter(Map<String, List<Uploadable>> map,Set<Uploadable> set, int num) {
		/**容纳错误消息的list*/
		List<Uploadable> mlist = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		/***
		 * 过滤掉所有的空格
		 */
		String mgr = StringUtil.trimAll(this.getMgr());
	    String daqu = StringUtil.trimAll(this.getDaqu());
	    String space = StringUtil.trimAll(this.getSpace());
	    String province =StringUtil.trimAll(this.getProvince());
	    String city =StringUtil.trimAll(this.getCity());
	    String country =StringUtil.trimAll(this.getCountry());
	    String product =StringUtil.trimAll(this.getProduct());
	    String isUpdate =StringUtil.trimAll(this.getIsUpdate());
	    String id =StringUtil.trimAll(this.getId());
	    
	    /**
	     * 判断是否导入的模板是正确的模板
	     */
	    if(num == 1) {
	    	if(!CollectionConstant.basic_mb.get(0).contains(mgr)
	    			||!CollectionConstant.basic_mb.get(1).contains(daqu)
	    			||!CollectionConstant.basic_mb.get(2).contains(space)
	    			||!CollectionConstant.basic_mb.get(3).contains(province)
	    			||!CollectionConstant.basic_mb.get(4).contains(city)
	    			||!CollectionConstant.basic_mb.get(5).contains(country)
	    			||!CollectionConstant.basic_mb.get(6).contains(product)
	    			||!CollectionConstant.basic_mb.get(7).contains(isUpdate)
	    			||!CollectionConstant.basic_mb.get(8).contains(id)) {
	    		MsgInfo msg = new MsgInfo(Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_NUM+num);
				mlist.add(msg);
				map.put("stop", mlist);
		    	return null;
		    }
	    } else {
	    	/***
	    	 * 获取项目上下文的共享数据
	    	 */
	    	ServletContext servletContext = ServletContextUtils.getServletContext();
	    	/***
	    	 * 判断重复项
	    	 */
	    	String basicKeys = ((HashSet<String>)servletContext.getAttribute("basicKeys")).toString();
	    	/***
		     * 数据合格性校验
		     */
			if(!CollectionConstant.cuFlags.contains(isUpdate)) {
				MsgInfo msg = new MsgInfo(isUpdate,"修订标识",CollectionConstant.cuFlags.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if("E".equals(isUpdate)) {
				if(StringUtil.isBlank(id)) {
					MsgInfo msg = new MsgInfo(isUpdate,"修订标识有误或id有误","修订标识为E或空白时，id不能为空。id为空白时，修订标识只能是N",Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			//省份不能为空
			if(StringUtil.isBlank(province)) {
				MsgInfo msg = new MsgInfo(province,"省份有误","省份不能为空",Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			//地市不能为空
			if(StringUtil.isBlank(city)) {
				MsgInfo msg = new MsgInfo(city,"地市有误","地市不能为空",Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			//区县不能为空
			if(StringUtil.isBlank(country)) {
				MsgInfo msg = new MsgInfo(country,"区县有误","区县不能为空",Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			
			/***
			 * 判断是否在数据库中存在
			 */
			if(isUpdate.equals("N")) {
				String key = province+"-"+city+"-"+country+"-"+product;
				if(basicKeys.contains(key)) {
					MsgInfo msg = new MsgInfo(key,Constant.REPEATABLE,"对应了多个经理，请检查",Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			} else if(isUpdate.equals("E")) {
				String key = province+"-"+city+"-"+country+"-"+product;
				if(basicKeys.contains(key)) {
					key = key+"-"+id;
					if(!basicKeys.contains(key)) {
						MsgInfo msg = new MsgInfo(key,Constant.REPEATABLE,"对应了多个经理，请检查",Constant.ERROR_NUM+num);
						mlist.add(msg);
					}
				}
			}
			
			/***
			 * 返回处理后的结果数据
			 */
			if(!mlist.isEmpty()) {
				map.put("fail"+num, mlist);
			} 
			/***把验证通过的合格数据返回**/
			this.setAll(mgr,daqu,space,province,city,country,product,isUpdate,id);
			//不是空行才添加
			if(StringUtil.isNotBlank(province)&&StringUtil.isNotBlank(city)&&StringUtil.isNotBlank(country)) {
				set.add(this);
			}
			return this;
	    }
	    return null;
	}
	private void setAll(String mgr, String daqu, String space, 
			String province, String city, String country,
			String product, String isUpdate, String id) {
		this.mgr=mgr;
		this.daqu=daqu;
		this.space=space;
		this.province=province;
		this.city=city;
		this.country=country;
		this.product=product;
		this.isUpdate=isUpdate;
		this.id=id;
	}
	
	@Override
	public String flag() {
		return this.isUpdate;
	}
}
